package com.shou.trigger.http;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shou.api.model.dto.DeleteDTO;
import com.shou.api.model.dto.PageQueryDTO;
import com.shou.api.model.dto.UserDTO;
import com.shou.api.model.response.ResultData;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.user.model.entity.UserEntity;
import com.shou.domain.user.service.IUserService;
import com.shou.trigger.http.mapstruct.UserDTOConvert;
import com.shou.trigger.http.mapstruct.common.PageQueryDTOConvert;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Validated
@RestController
@CrossOrigin
@RequestMapping("/api/v1/user")
public class UserController extends AbstructController{
    @Resource
    private IUserService userService;
    @Resource
    private PageQueryDTOConvert pageQueryDTOConvert;
    @Resource
    private UserDTOConvert userDTOConvert;

    /**
     * 分页查询
     */
    @GetMapping("/user-info")
    public ResultData<IPage<UserDTO>> page(@RequestParam Map<String, Object> rulePageQueryDTOMap){
        PageQueryDTO pageQueryDTO = PageQueryDTO.getRulePageQueryDTOFromMap(rulePageQueryDTOMap);
        // 入参校验
        validateQueryParams(pageQueryDTO);
        // 将DTO对象转换为实体类
        PageQueryEntity pageQueryEntity = pageQueryDTOConvert.convertPageQueryDTOToEntity(pageQueryDTO);
        // 分页查询
        IPage<UserEntity> queueMessageList = userService.pageQuery(pageQueryEntity);
        return ResultData.success(userDTOConvert.convertUserEntityPageToDTO(queueMessageList));
    }

    /**
     * 新增用户信息
     */
    @PostMapping("/user-info")
    public ResultData<String> insertUser(@RequestBody UserDTO userDTO){
        // 将DTO对象转换为Entity对象，调用domain层方法
        UserEntity userEntity = userDTOConvert.convertUserDTOToEntity(userDTO);
        // 保存(新增)规则
        userService.insertUser(userEntity);
        return ResultData.success("insertUser success");
    }

    /**
     * 删除单/多个用户信息
     */
    @DeleteMapping("/user-info")
    public ResultData<String> deleteUserByIds(@RequestBody @Validated DeleteDTO deleteDTO) {
        // 根据传递的ID(可以是多个)删除用户信息
        userService.deleteByIds(deleteDTO.getIds());
        return ResultData.success("deleteUserByIds success");
    }

    /**
     * 清空所有用户信息
     */
    @DeleteMapping("/all-user-info")
    public ResultData<String> deleteAll() {
        // 清空用户信息表
        userService.deleteAll();
        return ResultData.success("deleteAll success");
    }

    /**
     * 根据用户ID更新用户信息
     */
    @PutMapping("/user-info")
    public ResultData<String> updateUserById(@RequestBody @Validated UserDTO rule) {
        // 将DTO对象转换为Entity对象，调用domain层方法
        UserEntity userEntity = userDTOConvert.convertUserDTOToEntity(rule);
        // 根据ID更新用户信息
        userService.updateUserById(userEntity);
        return ResultData.success("updateUserById success");
    }
}